package com.qk.interceptor;

import com.qk.utils.JwtUtils;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
@Slf4j
//@Component
public class TokenInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String path = request.getRequestURI();
        //判断是否是LOGIN
        if (path.contains("login")) {
            //放行
            return true;
        }
        //获取token
        String token = request.getHeader("token");
        //判断token的健壮性
        if (token == null || token.isEmpty()) {
            log.info("token为空");
            response.setStatus(401);
            return false;
        }
        //解析token，判断token是否非法
        try {
            JwtUtils.parseToken(token);
        }
        catch (Exception e) {
            log.info("token为非法,解析失败");
            response.setStatus(401);
            return false;

        }
        //放行
       return true;
    }
    }



